using System.Collections.Generic;
using Framework;
using UnityEngine;
//可以实现界面后退功能
public class UIBackFeature : IUGUIFeature, IActionInputCallback
{
    private List<string> _floatPath = new List<string>(10);
    private List<string> _popPath = new List<string>(10);
    private bool _processing;
    public UIBackFeature()
    {
        GameEntry.DeviceManager.AddActionCallback(this);
    }
    private void guiBack()
    {
        if (!_processing)
        {
            if (_popPath.Count > 0)
            {
                var path = _popPath[_popPath.Count - 1];
                GameEntry.GUI.Close(path);
            }
            else if (_floatPath.Count > 0)
            {
                var path = _floatPath[_floatPath.Count - 1];
                GameEntry.GUI.Close(path);
            }
        }
    }
    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.U))
        {
            GameEntry.Table.Lang.Branch = "ch1";
            GameEntry.Coroutine.Start(GameEntry.Table.LoadLang(GameEntry.AssetsLoader, () =>
            {
                Debug.LogError("updateall");
                TextLang.UpdateAll();
            }));
        }
    }
    public void OnProcessing(int count)
    {
        _processing = count > 0;
    }

    public void SetShowStatus(UIWindow target, UIStatus value)
    {
    }

    public void SetStatus(UIWindow target, UIStatus value, UIStatus previous)
    {
        var layer = target.Window.Layer;
        if (value == UIStatus.OpenAfter)
        {
            if (layer == 1)
            {
                _floatPath.Add(target.Path);
            }
            else if (layer == 2)
            {
                _popPath.Add(target.Path);
            }
        }
        if (value == UIStatus.Close && previous == UIStatus.None)
        {
            if (layer == 1)
            {
                _floatPath.Remove(target.Path);
            }
            else if (layer == 2)
            {
                _popPath.Remove(target.Path);
            }
        }
    }

    public void OnActionInputStart(InputAction action)
    {
    }

    public void OnActionInputPerform(InputAction action)
    {
    }

    public void OnActionInputCancel(InputAction action)
    {
        if (GameInputName.Action_B == action.name)
        {
            guiBack();
        }
    }

    public void OnActionHoldPerform(InputAction action)
    {
    }

    public void OnResort()
    {

    }

}